package com.sitanyun.merchant.guide.frament.view.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.sitanyun.merchant.guide.R;
import com.sitanyun.merchant.guide.adapter.BankAdapter;
import com.sitanyun.merchant.guide.base.BaseActivity;
import com.sitanyun.merchant.guide.bean.BankBean;
import com.sitanyun.merchant.guide.bean.DeleteBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.url.Urls;
import com.sitanyun.merchant.guide.weiht.SharedPreferenceUtil;
import com.sitanyun.merchant.guide.weiht.SpaceItemDecoration;
import com.zhy.http.okhttp.OkHttpUtils;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 银行卡
 */
public class BankActivity extends BaseActivity {


    @BindView(R.id.in_bank)
    TextView inBank;
    @BindView(R.id.rv_bank)
    RecyclerView rvBank;
    @BindView(R.id.no_bank)
    TextView noBank;
    @BindView(R.id.shop_inter)
    TextView shopInter;
    private String isIndividual;
    private BankAdapter bankAdapter;
    private BankBean bankBean;
    private boolean as=true;

    @Override
    public int getLayoutId() {
        return R.layout.activity_bank;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        setFindViewById(true);
        setTitleStr("银行卡管理");
        isIndividual = getIntent().getStringExtra("isIndividual");
    }

    /**
     * 显示
     */
    @Override
    protected void onResume() {
        super.onResume();
        OkHttpUtils.get().url(Urls.card)
                .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                .addHeader("Content-Type", "application/json")
                .addParams("individualId", isIndividual).build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                bankBean = new Gson().fromJson(response, BankBean.class);
                if (bankBean.getCode() == 0) {
                    if (bankBean.getData().size() == 0) {
                        rvBank.setVisibility(View.GONE);
                        noBank.setVisibility(View.VISIBLE);
                        shopInter.setVisibility(View.GONE);
                    } else {
                        shopInter.setVisibility(View.VISIBLE);
                        shopInter.setText("您共添加"+bankBean.getData().size()+"张银行卡");
                        rvBank.setVisibility(View.VISIBLE);
                        noBank.setVisibility(View.GONE);
                        bankAdapter.setNewData(bankBean.getData());
                        bankAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

    }

    @Override
    public void initView() {
        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(BankActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvBank.setLayoutManager(layoutManager);
        bankAdapter = new BankAdapter(BankActivity.this);
        rvBank.setAdapter(bankAdapter);
        //设置item间距，
        rvBank.addItemDecoration(new SpaceItemDecoration(20));
        bankAdapter.notifyDataSetChanged();
        //点击事件
        bankAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.bank_delete:
                        //删除银行卡
                        dialogout(position);

                        break;
                    case R.id.look:

                        ImageView looks= (ImageView) view.findViewById(R.id.look);

                        if (as){
                            Glide.with(BankActivity.this).load(R.mipmap.look).into(looks);
                        as=false;

                           bankAdapter.setPosition(0);
                            bankBean.getData().get(position).setDelFlag("1");
                        }else {
                           bankAdapter.setPosition(1);
                            bankBean.getData().get(position).setDelFlag("0");
                            as=true;
                            Glide.with(BankActivity.this).load(R.mipmap.nolook).into(looks);
                        }
                        bankAdapter.notifyDataSetChanged();
                        break;

                }

            }
        });

    }    //删除银行卡
    private void dialogout(int position) {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(BankActivity.this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(BankActivity.this, R.layout.loginoutdialog_item, null);
        dialog.setContentView(view);
        TextView ts_message= (TextView) view.findViewById(R.id.ts_message);
       TextView cancel= (TextView) view.findViewById(R.id.cancel);
        TextView define= (TextView) view.findViewById(R.id.define);
        ts_message.setText("确定删除此银行卡信息？");
        cancel.setText("删除");
        cancel.setTextColor(getColor(R.color.red));
        define.setTextColor(getColor(R.color.text));
        define.setText("取消");
        view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                OkHttpUtils.get().url(Urls.delcard)
                        .addHeader("Authorization", "Bearer " + SharedPreferenceUtil.getStringData("token"))
                        .addHeader("Content-Type", "application/json")
                        .addParams("cardId", bankBean.getData().get(position).getCardId()).build().execute(new StringCallbacks() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        DeleteBean bankBean = new Gson().fromJson(response, DeleteBean.class);
                        if (bankBean.getCode() == 0) {
                            onResume();
                        }
                    }
                });
            }
        });
        view.findViewById(R.id.define).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
            }
        });


        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        //  window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        //设置弹出动画
        //   window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
        /*添加View到当前显示的Activity*/
        //   MerchantApp.getInstance().showView(view);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.in_bank)
    public void onViewClicked() {
        startActivity(new Intent(BankActivity.this, BankInsertActivity.class).putExtra("isIndividual", isIndividual));
    }
}
